//
//  BKHistoryViewController.m
//  baokaodaxue
//
//  Created by dangyangyang on 15/6/1.
//  Copyright (c) 2015年 宏图教育. All rights reserved.
//

#import "BKHistoryViewController.h"
#import <ReactiveCocoa.h>
#import <RACEXTScope.h>
#import "BKSearchField.h"
#import "BKHistoryViewModel.h"
#import "BKResultOfHistoryViewController.h"
#import <MBProgressHUD.h>

@interface BKHistoryViewController ()
@property (weak, nonatomic) IBOutlet BKSearchField *kaoqu;
@property (weak, nonatomic) IBOutlet BKSearchField *wenli;
@property (weak, nonatomic) IBOutlet BKSearchField *suozaidi;
@property (weak, nonatomic) IBOutlet BKSearchField *xuexiao;
@property (weak, nonatomic) IBOutlet BKSearchField *pici;

@end

@implementation BKHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    _suozaidi.flag = 8000;
    _kaoqu.flag = 8000;
    
    @weakify(self);
    [RACObserve(self.suozaidi, text) subscribeNext:^(id x) {
        @strongify(self);
        if ([x isKindOfClass:[NSString class]]&& [x length]>0) {
            self.xuexiao.colleageArray = @[];
            
            [BKHistoryViewModel colleageWithProv:x success:^(NSArray *dataArray) {
                self.xuexiao.colleageArray = dataArray;
                self.xuexiao.flag = 8004;
                
        
            } failure:^(NSString *message) {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                [alert show];
                
            }];
        }
        
    }];
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    if ([userDefaults objectForKey:@"kq"] ) {
        _kaoqu.text = [userDefaults objectForKey:@"kq"];
        
    }
    
    if ( [userDefaults objectForKey:@"dq"]) {
        _suozaidi.text = [userDefaults objectForKey:@"dq"];
    }

    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)p_back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (IBAction)p_submit:(id)sender {
    
    NSString *kq = _kaoqu.text;
    if ( [kq isKindOfClass:[NSString class]] && [kq length]==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择考区" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    
    NSString *xuexiao = _xuexiao.text;
    if ([xuexiao isKindOfClass:[NSString class]]&& [xuexiao length]==0) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请选择学校" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    
    __block NSString *cid = @"";
    NSArray *colleageArray = _xuexiao.colleageArray;
    [colleageArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        if ([obj isKindOfClass:[NSDictionary class]]) {
            if ([obj[@"name"] isEqualToString:_xuexiao.text] ) {
                *stop = YES;
                cid = obj[@"cid"];
            }
        }
    }];
   
    if ([cid isEqual:[NSNull null]]) {
        
        NSString *mes = [NSString stringWithFormat:@"该学校不在%@，请重新选择学校",_suozaidi.text];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:mes delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    
    
    NSString *pc = _pici.text;
    
     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:_kaoqu.text forKey:@"kq"];
    [userDefaults setObject:_suozaidi.text forKey:@"dq"];
    MBProgressHUD *hud = [[MBProgressHUD alloc] initWithView:self.view];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.labelText = @"正在提交";
    [self.view addSubview:hud];
    [hud show:YES];
    
    __weak BKHistoryViewController *weakSelf = self;
    [BKHistoryViewModel historyWithKaoQu:_kaoqu.text
                                   wenli:_wenli.text
                                    diqu:_suozaidi.text
                                     cid:cid
                                    pici:pc
                                 success:^(NSDictionary *dic) {
        [hud hide:YES];
        BKResultOfHistoryViewController *result = [[BKResultOfHistoryViewController alloc] initWithNibName:NSStringFromClass([BKResultOfHistoryViewController class]) bundle:nil];
        result.cid = cid;
        
        NSDictionary *provDic = @{
                                   @"北京":@"1",
                                   @"天津":@"2",
                                   @"上海":@"3",
                                   @"重庆":@"4",
                                   @"河北":@"5",
                                   @"河南":@"6",
                                   @"山东":@"7",
                                   @"山西":@"8",
                                   @"安徽":@"9",
                                   @"江西":@"10",
                                   @"江苏":@"11",
                                   @"浙江":@"12",
                                   @"湖北":@"13",
                                   @"湖南":@"14",
                                   @"广东":@"15",
                                   @"广西":@"16",
                                   @"云南":@"17",
                                   @"贵州":@"18",
                                   @"四川":@"19",
                                   @"陕西":@"20",
                                   @"青海":@"21",
                                   @"宁夏":@"22",
                                   @"黑龙江":@"23",
                                   @"吉林":@"24",
                                   @"辽宁":@"25",
                                   @"西藏":@"26",
                                   @"新疆":@"27",
                                   @"内蒙古":@"28",
                                   @"海南":@"29",
                                   @"福建":@"30",
                                   @"甘肃":@"31"};
        result.provid = provDic[self.kaoqu.text];
        
        result.name = weakSelf.xuexiao.text;
        result.historyDic = dic;
        [weakSelf.navigationController pushViewController:result animated:YES];
        
        
    } failure:^(NSString *message) {
        [hud hide:YES];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
        [alertView show];

    }];
    
}


@end
